package com.kdtech.analyse.news;
import com.kdtech.utils.DateUtils;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.analyse.tool.ParseLogic;
import com.kdtech.analyse.tool.ParseTool;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.StringUtils;
import com.kdtech.utils.HtmlCleaner;

public class ChinahgnewsNewsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		boolean bRet=false;
		String[] regex={
				"http://hegangnews.dbw.cn/.*/[0-9]*/[0-9]*/[0-9]*/[0-9]*.shtml",

				};
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				bRet=true;
				break;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}		
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();		
				
		news.setUrl(url);		
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("h1").text();
		content=HtmlCleaner.getContentHtml(url,doc.select("div.news"));

		/*
		 * 作者
		 */
		author=JSoupUtils.matchAuthor(doc, "来源：");

		date=DateUtils.matchDate(doc.select("div.tem_wwwbox").text());

		if(StringUtils.isBlank(title)){
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);	
		
		return news;
	}
	
	public NewsMeta Update(NewsMeta meta) {
		return null;
	}
	public static void main(String[] args) {
		String url="http://hegangnews.dbw.cn/system/2016/11/08/057432766.shtml";
		url=UrlArgumentTop.FromatUrl(url);
		System.out.println(url);
		UrlMeta urlMeta=CrawlHTML.responseToURL(url);
		ChinahgnewsNewsAnalyse test=new ChinahgnewsNewsAnalyse();
		boolean detailPage=test.isDetailPage(url);
		if(detailPage){
		NewsMeta parserHtml =test.parserHtml(urlMeta);
		System.out.println(parserHtml);
		}else{
			System.out.println("不符合正则");
		}
	}



	
	public boolean isNeedUpdate(){
		return false;
	}

}
